﻿#region License
/* Copyright (C) 2009 Tim Coulter
 * 
 * This file is part of ExtremeML.
 * 
 * ExtremeML is free software: you can redistribute
 * it and/or modify it under the terms of the GNU General Public
 * License, as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 * 
 * This software is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty
 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
 * the GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program. If not, see http://www.gnu.org/licenses/.
*/
#endregion

using System;
using System.Collections.ObjectModel;
using DocumentFormat.OpenXml;
using ExtremeML.Core;

namespace ExtremeML.Spreadsheet.Address
{
	/// <summary>
	/// Encapsulates a collection of <see cref="CellArea"/> that represent a sequence of references.
	/// </summary>
	public class CellAreaCollection : Collection<CellArea>, IWrapper
	{
		#region Housekeeping

		/// <summary>
		/// Initializes a new instance of the <see cref="CellAreaCollection"/> class.
		/// </summary>
		internal CellAreaCollection()
			: this(false)
		{
		}

		/// <summary>
		/// Initializes a new instance of the <see cref="CellAreaCollection"/> class.
		/// </summary>
		/// <param name="eagerMode">A <see cref="bool"/> that indicates whether member <see cref="CellArea"/> instances should be configured in eager expansion mode.</param>
		internal CellAreaCollection(
			bool eagerMode)
		{
			EagerMode = eagerMode;
		}

		/// <summary>
		/// Initializes a new instance of the <see cref="CellAreaCollection"/> class.
		/// </summary>
		/// <param name="list">A <see cref="ListValue{StringValue}"/> that represents the address references of members.</param>
		internal CellAreaCollection(
			ListValue<StringValue> list)
			: this (list, false)
		{
		}

		/// <summary>
		/// Initializes a new instance of the <see cref="CellAreaCollection"/> class.
		/// </summary>
		/// <param name="list">A <see cref="ListValue{StringValue}"/> that represents the address references of members.</param>
		/// <param name="eagerMode">A <see cref="bool"/> that indicates whether member <see cref="CellArea"/> instances should be configured in eager expansion mode.</param>
		internal CellAreaCollection(
			ListValue<StringValue> list,
			bool eagerMode)
		{
			if (list == null) throw new ArgumentNullException("list");

			EagerMode = eagerMode;

			foreach (var item in list.Items)
			{
				Add(AddressParser.ParseCellArea(item.Value));				
			}
		}

		/// <summary>
		/// Overrides the base class to ensure that new members are coerced
		/// to match the eager expansion mode defined by this instance.
		/// </summary>
		protected override void InsertItem(
			int index,
			CellArea item)
		{
			item.EagerMode = EagerMode;

			base.InsertItem(index, item);
		}

		#endregion
		#region Properties

		/// <summary>
		/// Gets or sets whether member instances expand when row or column
		/// insertion occurs immediately below or to the right of them.
		/// </summary>
		public bool EagerMode { get; private set; }

		#endregion
		#region Interface Implementation
		#region IWrapper

		/// <summary>
		/// Returns a value that indicates whether this instance wraps content that should be saved to the document.
		/// </summary>
		public bool IsPopulated()
		{
			return Items.Count > 0;
		}

		/// <summary>
		/// Establishes relationships with other package content objects.
		/// </summary>
		public void Activate()
		{
			// This class requires no activation.		
		}

		/// <summary>
		/// Safely terminates relationships with other package content objects.
		/// </summary>
		public void Deactivate()
		{
			// This class requires no deactivation.		
		}

		#endregion
		#endregion
		#region Overrides


		#endregion
		#region Events


		#endregion
	}
}
